package com.tuanzi.loan.business.service.settlement.impl;

import com.tuanzi.loan.business.entity.settlement.SwMoneyFlow;
import com.tuanzi.loan.business.repository.settlement.SwMoneyFlowRepository;
import com.tuanzi.loan.business.service.settlement.SwMoneyFlowService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.service.CommonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class SwMoneyFlowServiceImpl extends CommonServiceImpl<SwMoneyFlow,Long> implements SwMoneyFlowService {

    @Autowired
    private SwMoneyFlowRepository swMoneyFlowRepository;


    @Override
    public SwMoneyFlow getLatestSwMoneyFlowByCompanyId(Long companyId) {
        List<SwMoneyFlow> swMoneyFlowList = swMoneyFlowRepository.getLatestSwMoneyFlowByCompanyId(companyId);
        if(swMoneyFlowList.size()==0){
            return null;
        }

        SwMoneyFlow swMoneyFlow = swMoneyFlowList.stream().max((a, b) -> a.getId().intValue() - b.getId().intValue()).get();
        return swMoneyFlow;
    }

    @Override
    protected JpaRepository<SwMoneyFlow, Long> getRepository() {
        return swMoneyFlowRepository;
    }
}
